# -*- coding: utf-8 -*-
"""
@time: 2020-01-07 下午 09:12
@author: leslie lee
"""
import sys, os  # os用于判断路径是否有重复的设置文件

if hasattr(sys, 'frozen'):
    os.environ['PATH'] = sys._MEIPASS + ";" + os.environ['PATH']

from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Fcq
from matplotlib.figure import Figure
from matplotlib.pyplot import rcParams

from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT

class CustomizeToolBar(NavigationToolbar2QT):
    """
    用来重构matplotlib类的工具栏
    若要修改,请看NavigationToolbar2QT.toolitems
    """
    def __init__(self, canvas_, parent_):
        self.toolitems = (
            ('Home', 'Lorem ipsum dolor sit amet', 'home', 'home'),
            ('Back', 'consectetuer adipiscing elit', 'back', 'back'),
            ('Forward', 'sed diam nonummy nibh euismod', 'forward', 'forward'),
            ('Pan', 'tincidunt ut laoreet', 'move', 'pan'),
            ('Zoom', 'dolore magna aliquam', 'zoom_to_rect', 'zoom'),
            ('Save', 'sollemnes in futurum', 'filesave', 'save_figure'),
            )
        NavigationToolbar2QT.__init__(self, canvas_, parent_)

rcParams['font.sans-serif'] = ['SimHei']  # 中文显示
rcParams['axes.unicode_minus'] = False  # 负号显示


class TimeCanvas:
    """
    定义时域图
    """

    def __init__(self):
        # 定义时域窗口
        self.time_widget = QWidget()

        # 定义时域图
        self.time_fig = Figure(figsize=(8, 10), dpi=110, facecolor='white')
        self.time_canvas = Fcq(self.time_fig)
        self.time_axes1 = self.time_fig.add_subplot(111)

        # 定义工具栏
        self.time_toolbar = CustomizeToolBar(self.time_canvas, self.time_widget)

        # 定义时域图布局
        self.time_widget_layout = QVBoxLayout()

        # 运行函数
        self.init_TimeCanvas()

    def init_TimeCanvas(self):
        # 定义窗口大小
        self.time_widget.resize(600, 400)  # 设置窗口大小

        # 时域图
        self.time_fig.subplots_adjust(top=0.915, bottom=0.155, left=0.11, right=0.955, hspace=0.2, wspace=0.2)
        self.time_axes1.set_title('Time Diagram', pad=0, loc='center', size=20, color='red')
        self.time_axes1.set_xlabel('Time(s)', fontsize='x-large', color='red')  # size = 10 也能设置字体大小
        self.time_axes1.set_ylabel('Amplitude(V)', fontsize='x-large', color='red')
        self.time_axes1.grid(ls='--')  # 生成网格
        self.time_canvas.draw()

        # 更新时域工具栏
        self.time_toolbar.update()

        # 时域图布局
        self.time_widget_layout.addWidget(self.time_toolbar)
        self.time_widget_layout.addWidget(self.time_canvas)
        self.time_widget_layout.setContentsMargins(0, 0, 0, 0)
        self.time_widget.setLayout(self.time_widget_layout)

        # 显示窗口
        self.time_widget.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    my_gui = TimeCanvas()
    sys.exit(app.exec_())
